package cn.topevery.garden.dal.logic;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.topevery.garden.dal.dao.ProjStageFileCodeDao;
import cn.topevery.garden.entity.dto.ProjPlanDTO;
import cn.topevery.garden.entity.dto.ProjStageFileCodeDTO;
import cn.topevery.garden.entity.po.ProjBasicInfo;
import cn.topevery.garden.entity.po.ProjPlan;
import cn.topevery.garden.entity.po.ProjStageFileCode;
import cn.topevery.garden.entity.query.ProjStageFileCodeQuery;

import cn.topevery.garden.entity.vo.ProjStageFileCodeVO;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.base.BaseEntityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

/**
 * 项目阶段附件编码维护表
 *
 * @author code-generate
 * @date 2022-11-02
 */
@Service
public class ProjStageFileCodeLogic extends BaseLogic<ProjStageFileCode> {
    @Autowired
    ProjStageFileCodeDao projStageFileCodeDao;

    @Override
    public BaseEntityDao<ProjStageFileCode, Integer> getDao() {
        return projStageFileCodeDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<ProjStageFileCodeVO> listPage(ProjStageFileCodeQuery query) {
        return projStageFileCodeDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        projStageFileCodeDao.delete(userId, ids);
    }

    public Boolean saveOrUpdate(String userId, ProjStageFileCodeDTO dto) {
        // 校验数据
        this.checkData(dto);

        ProjStageFileCode projStageFileCode = new ProjStageFileCode();
        BeanUtil.copyProperties(dto, projStageFileCode);

        // 保存数据
        this.projStageFileCodeDao.saveOrUpdate(userId, projStageFileCode);
        return true;
    }

    private void checkData(ProjStageFileCodeDTO dto) {
        if (ObjectUtil.isEmpty(dto)) {
            throw new RuntimeException("传入的对象不能为空！");
        }
        if (ObjectUtil.isEmpty(dto.getProjStageType())) {
            throw new RuntimeException("项目阶段类型不能为空！");
        }
        if (ObjectUtil.isEmpty(dto.getTypeCode())) {
            throw new RuntimeException("项目附件类型编码不能为空！");
        }
        if (ObjectUtil.isEmpty(dto.getTypeName())) {
            throw new RuntimeException("项目附件类型名称不能为空！");
        }
        // 当不为空时为修改
        if (ObjectUtil.isNotEmpty(dto.getId())) {
            // 查询是否存在
            ProjStageFileCode projStageFileCode = this.getEntity(dto.getId());
            if (ObjectUtil.isEmpty(projStageFileCode)) {
                throw new RuntimeException("该信息在系统不存在，请检查或刷新数据！");
            }
        }
    }

    public List<ProjStageFileCodeVO> getByStageTypeCode(String stageTypeCode){
        if (ObjectUtil.isEmpty(stageTypeCode)) {
            throw new RuntimeException("传入的阶段类型编码不能为空！");
        }
        return this.projStageFileCodeDao.getByStageTypeCode(stageTypeCode);
    }

}
